﻿using System;
using System.Collections.Generic;
using System.Text;
using OC.Common.Interface;
using System.Collections;
using OC.Common.Entity;

namespace OC.Logic
{
    class DictionaryLogic : OC.Common.Interface.IDictionary
    {

        public Dictionary<string, List<string>> GetAllList(string type)
        {
            Hashtable table = new Hashtable();
            //IList list = DataHelper.Instance().GetList("DictionaryLogic_GetAllList", table);
            IList list = GetListByDictionaryType("DictionaryLogic_GetListByDictionaryType", type);
            Dictionary<string, List<string>> allDicID = GetChildsIDByDictionaryID(list);
            return allDicID;
        }

        public IList GetDictionaryRelationByRelation_Type(string type, string dicID)
        {
            Hashtable table = new Hashtable();
            table.Add("relation_type", type);
            table.Add("source_dictionary_id", dicID);
            IList DicRelationList = DataHelper.Instance().GetList("DictionaryLogic_GetDictionaryRelationByRelation_Type", table);
            return DicRelationList;
        }

        public IList GetUserByUserIDs(List<string> userIds)
        {
            string userIDs = JoinList(userIds);
            if (!string.IsNullOrEmpty(userIDs))
            {
                Hashtable table = new Hashtable();
                table.Add("user_ids", userIDs);
                IList UserList = DataHelper.Instance().GetList("DictionaryLogic_GetUserByUserIDs", table);
                return UserList;
            }
            return new List<UserEntity>();
        }

        public IList GetDictionaryByIDs(List<string> dicIds)
        {
            string dicID = JoinList(dicIds);
            if (!string.IsNullOrEmpty(dicID))
            {
                Hashtable table = new Hashtable();
                table.Add("dictionary_ids", dicID);
                IList DicList = DataHelper.Instance().GetList("DictionaryLogic_GetDictionaryByIDs", table);
                return DicList;
            }
            return new List<DictionaryEntity>();
        }

        public IList GetUserOrganizationByUserID(string userID)
        {
            Hashtable table = new Hashtable();
            table.Add("user_id", userID);
            IList DicList = DataHelper.Instance().GetList("DictionaryLogic_GetUserOrganizationByUserID", table);
            return DicList;
        }


        public IList GetUserByDictionaryID(List<string> dicIDs, string userName)
        {
            string dicID = JoinList(dicIDs);
            Hashtable table = new Hashtable();
            table.Add("dictionary_ids", dicID);
            IList userRelationList = DataHelper.Instance().GetList("DictionaryLogic_GetUserRelationByDictionaryID", table);
            IList list = GetUsers(userRelationList, userName);
            return list;
        }

        private string JoinList(List<string> dicIDs)
        {
            string dicID = string.Empty;
            foreach (string id in dicIDs)
            {
                dicID += "'" + id.Trim() + "',";
            }
            if (!string.IsNullOrEmpty(dicID))
            {
                dicID = dicID.TrimEnd(',');
            }
            return dicID;
        }

        private IList GetUsers(IList userRelationList, string userName)
        {
            string userIds = string.Empty;
            foreach (UserRelationEntity item in userRelationList)
            {
                userIds += "'" + item.USER_ID + "',";
            }
            IList list = null;
            if (!string.IsNullOrEmpty(userIds))
            {
                userIds = userIds.TrimEnd(',');
                Hashtable tableUserCondition = new Hashtable();
                tableUserCondition.Add("user_ids", userIds);
                tableUserCondition.Add("cnName", userName);
                tableUserCondition.Add("enName", userName);
                list = DataHelper.Instance().GetList("DictionaryLogic_GetUserByUserIDsAndUserName", tableUserCondition);
            }
            return list;
        }

        private Dictionary<string, List<string>> GetChildsIDByDictionaryID(IList dicEntitys)
        {
            Dictionary<string, List<string>> allDicID = new Dictionary<string, List<string>>();

            foreach (DictionaryEntity dicEntity in dicEntitys)
            {
                if (dicEntity.PARENT_DICTIONARY_ID.Trim() == "0")
                {
                    List<string> ids = new List<string>();
                    ids.Add(dicEntity.DICTIONARY_ID);
                    allDicID.Add(dicEntity.DICTIONARY_ID, ids);
                    GetChildsID(dicEntitys, allDicID, ids, dicEntity.DICTIONARY_ID);
                    break;
                }
            }
            return allDicID;
        }
        public bool DeleteDictionaryByID(string dicID)
        {
            Hashtable table = new Hashtable();
            table.Add("dictionary_id", dicID);
            if (DataHelper.Instance().Delete("DictionaryLogic_DeleteDictionaryByID", table) > 0)
            {
                return true;
            }
            return false;
        }

        private void GetChildsID(IList dicEntitys, Dictionary<string, List<string>> allDicID, List<string> ids, string pID)
        {
            foreach (DictionaryEntity item in dicEntitys)
            {
                foreach (List<string> idValue in allDicID.Values)
                {
                    if (idValue.Contains(item.PARENT_DICTIONARY_ID))
                    {
                        if (!idValue.Contains(item.DICTIONARY_ID))
                        {
                            idValue.Add(item.DICTIONARY_ID);
                        }
                    }
                }
                if (pID == item.PARENT_DICTIONARY_ID)
                {
                    if (!ids.Contains(item.DICTIONARY_ID))
                    {
                        ids.Add(item.DICTIONARY_ID);
                    }
                    if (!allDicID.ContainsKey(item.DICTIONARY_ID))
                    {
                        List<string> cIds = new List<string>();
                        cIds.Add(item.DICTIONARY_ID);
                        allDicID.Add(item.DICTIONARY_ID, cIds);
                        GetChildsID(dicEntitys, allDicID, cIds, item.DICTIONARY_ID);
                    }
                }
            }
        }

        public IList GetList(string parentId)
        {
            Hashtable table = new Hashtable();
            table.Add("parent_dictionary_id", parentId);
            IList list = DataHelper.Instance().GetList("DictionaryLogic_GetList", table);
            return list;
        }

        public IList GetListByDictionaryType(string where, string type)
        {
            Hashtable table = new Hashtable();
            table.Add("dictionary_type", type);
            IList list = DataHelper.Instance().GetList(where, table);
            return list;
        }
        public DictionaryEntity Get(string dicId)
        {
            Hashtable table = new Hashtable();
            table.Add("dictionary_id", dicId);
            return DataHelper.Instance().Get<DictionaryEntity>("DictionaryLogic_Get", table);
        }

        public void InsertDictionaryRelation(List<DictionaryRelationEntity> entitys)
        {
            foreach (DictionaryRelationEntity item in entitys)
            {
                Hashtable table = new Hashtable();
                table.Add("RELATION_ID", Guid.NewGuid().ToString());
                table.Add("SOURCE_DICTIONARY_ID", item.SOURCE_DICTIONARY_ID);
                table.Add("DESTINATION_DICTIONARY_ID", item.DESTINATION_DICTIONARY_ID);
                table.Add("RELATION_TYPE", item.RELATION_TYPE);
                table.Add("CREATE_BY", item.CREATE_BY);
                table.Add("CREATE_DATE", item.CREATE_DATE);
                DataHelper.Instance().Insert("DictionaryLogic_InsertDictionaryRelation", table);
            }
        }
        public void InsertDictionary(DictionaryEntity entity)
        {
            Hashtable table = new Hashtable();
            table.Add("DICTIONARY_ID", entity.DICTIONARY_ID);
            table.Add("CN_NAME", entity.CN_NAME);
            table.Add("EN_NAME", entity.EN_NAME);
            table.Add("DICTIONARY_TYPE", entity.DICTIONARY_TYPE);
            table.Add("DICTIONARY_CODE", entity.DICTIONARY_CODE);
            table.Add("PARENT_DICTIONARY_ID", entity.PARENT_DICTIONARY_ID);
            table.Add("ORDER_NO", "");
            table.Add("CREATE_DATE", entity.CREATE_DATE);
            table.Add("CREATE_BY", entity.CREATE_BY);
            table.Add("UPDATE_DATE", entity.UPDATE_DATE);
            table.Add("UPDATE_BY", entity.UPDATE_BY);
            table.Add("EFFECT_FROM", entity.EFFECT_FROM);
            table.Add("EFFECT_TO", entity.EFFECT_TO);
            table.Add("IS_ACTIVE", entity.IS_ACTIVE);
            table.Add("EXT01", entity.EXT01);
            table.Add("EXT15", entity.EXT15);
            DataHelper.Instance().Insert("DictionaryLogic_InsertDictionary", table);

        }

        public void UpdateDictionary(DictionaryEntity entity)
        {
            Hashtable table = new Hashtable();
            table.Add("CN_NAME", entity.CN_NAME);
            table.Add("UPDATE_DATE", entity.UPDATE_DATE);
            table.Add("UPDATE_BY", entity.UPDATE_BY);
            table.Add("EXT15", entity.EXT15);
            table.Add("DICTIONARY_ID", entity.DICTIONARY_ID);
            DataHelper.Instance().Update("DictionaryLogic_UpdateDictionary", table);
        }

        public bool Exists(string taskId)
        {
            throw new NotImplementedException();
        }

        public void DeleteRelationDictionaryByID(string DicId, string type)
        {
            Hashtable table = new Hashtable();
            table.Add("source_dictionary_ids", DicId);
            table.Add("relation_type", type);
            DataHelper.Instance().Get<DictionaryEntity>("DictionaryLogic_DeleteRelationDictionaryByID", table);
        }

    }
}
